
local jass = require 'jass.common'
local japi = require 'jass.japi'

local texture = class.panel:builder {
    parent = ac.frame.unitdetail,
    x = 523,
    y = 1030,
    w = 170,
    h = 18,
    normal_image = [[ReplaceableTextures\CameraMasks\Black_mask.blp]],
    life1 = {
        y = 2,
        x = -20,
        w = 100,h = 16,
        type = 'text',
        font_size = 12,
        align = 6,
    },
    life2 = {
        y = 2,
        x = 80,
        w = 120,h = 16,
        type = 'text',
        font_size = 12,
        align = 4,
    },
}


function texture:update()
    local unit = ac.unit:localunit()
    if unit == nil then 
        self.life1:set_text('')
        self.life2:set_text('')
        return 
    end 
    if self:is_show() then
        if unit:has_restriction '无敌' then
            self.life1:set_text('秘')
            self.life2:set_text('密')
            return
        end
        self.life1:set_text(('%s '):format(format_number1(unit:get '生命')))
        self.life2:set_text(('/ %s'):format(format_number1(unit:get '生命上限')))
    end
end

ac.hardware:event '界面绘制'(function()
    texture:update()
end)


--模拟数据防止数据魔兽溢出
local unit_attribute = require 'types.unit_attribute'
local register = unit_attribute.register

register('生命上限',{
    set = function(self,life)
    end,
})
register('生命',{
    set = function(self,life)
        local max = jass.GetUnitState(self.handle, jass.UNIT_STATE_MAX_LIFE)
        if life > 1 then
            local num = life/self:get '生命上限'*max
            jass.SetWidgetLife(self.handle, math.max(num,2))
        else
            jass.SetWidgetLife(self.handle, 1)
        end
    end,
})
